package com.atXiao.video.controller;

import com.atXiao.video.common.entity.ResBody;
import com.atXiao.video.entity.req.common.MessageDto;
import com.atXiao.video.entity.req.subscribe.SubscribeReq;
import com.atXiao.video.entity.resp.notify.NotifyResp;
import com.atXiao.video.service.SubscribeService;
import com.atXiao.video.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 关注、取关相关Controller
 */
@RequestMapping("/subscription")
@RestController
public class SubscribeController {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    SubscribeService subscribeService;


    /**
     * 订阅用户
     * @param subscribeReq
     * @return
     */
    @PostMapping("/subscribe")
    public ResBody followUser(@RequestBody SubscribeReq subscribeReq) {
        ResBody resBody = new ResBody();
        long start = System.currentTimeMillis();

        //TODO 公共参数校验(未来采用更为优雅的方式替换jsr303校验，或者挨个获取，判断为空，用一种对业务代码无侵入的方式)
        //执行业务
        try {
            resBody=subscribeService.followerUser(subscribeReq);
        } catch (Exception exception) {
            exception.printStackTrace();
            return ResBody.error();
        }
        long end = System.currentTimeMillis();
        //TODO: 统计日志
        return resBody;
    }


    /**
     * 获取通知消息
     * @return
     */
    @PostMapping("/getNotifyMsg")
    public ResBody getNotifyMsg(@RequestBody SubscribeReq subscribeReq){
        ResBody resBody = new ResBody();
        long start = System.currentTimeMillis();

        //TODO 公共参数校验(未来采用更为优雅的方式替换jsr303校验，或者挨个获取，判断为空，用一种对业务代码无侵入的方式)
        //执行业务
        String youtuber = subscribeReq.getYoutuber();
        try {
            NotifyResp notifyResp = subscribeService.getNotifyMsg(youtuber);
            resBody.setData(notifyResp);
        }catch (Exception exception){}

        long end = System.currentTimeMillis();
        //TODO: 统计日志
        return resBody;
    }


    /**
     * 清空消息
     * @return
     */
    @PostMapping("/clearNotifyMsg")
    public ResBody clearNotifyMsg(@RequestBody SubscribeReq subscribeReq){
        ResBody resBody = new ResBody();
        long start = System.currentTimeMillis();

        //TODO 公共参数校验(未来采用更为优雅的方式替换jsr303校验，或者挨个获取，判断为空，用一种对业务代码无侵入的方式)
        //执行业务
        String youtuber = subscribeReq.getYoutuber();
        try {
            subscribeService.clearNotifyMsg(youtuber);
        }catch (Exception exception){}

        long end = System.currentTimeMillis();
        //TODO: 统计日志
        return resBody;
    }










}
